<!--
 * @Author: zegobuilder zegobuilder@zego.im
 * @Date: 2022-10-20 17:58:51
 * @LastEditors: zegobuilder zegobuilder@zego.im
 * @LastEditTime: 2022-10-20 18:59:45
 * @FilePath: /undefined/Users/zego/Library/Containers/com.tencent.WeWorkMac/Data/Documents/Profiles/A83482F61442B94B322D0D3F72C3D415/Caches/Files/2022-10/2d800b6848b11ec49ad9f9cdc4c53c27/removeDeviceSoundLevel的副本.html
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Zego Express Video Call</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <!-- <script src="./dist/ZegoExpressWebRTC.js"></script> -->
        <script src="./js/ZegoExpressWebRTC-2.17.0.js"></script>
     
    </head>
    <body>
        <h1>Zego RTC Video Call</h1>
        <div class="video-wrapper">
            <hr>
            <h4>Local video</h4>
            <h4>Remote video</h4>
            <video id="local-video" autoplay muted playsinline controls></video>
            <!-- <video id="remote-video" autoplay muted playsinline controls></video> -->
            <div id="rv"></div>
        </div>
        <div id="information"></div>
    </body>
    <script>
        //初始化信息
        let appID = 1739272706;
        let server = 'wss://webliveroom-test.zego.im/ws';

        let userID = 'alagong'; // userID 用户自己设置，必须保证全局唯一
        let userName = "阿拉贡";// userName 用户自己设置，没有唯一性要求
        let roomID = "room02"; // roomID 用户自己设置，必须保证全局唯一

        let localStream = null;
        let accessToken = '';
        
        // 初始化实例
        const zg = new ZegoExpressEngine(appID, server);
        zg.setDebugVerbose(false);

        zg.on('roomStateUpdate', async (roomID, state, errorCode, extendedData) => {
            console.error('[roomStateUpdate]', roomID, state, errorCode, extendedData);
            if (state == 'CONNECTED') {
                localStream = await zg.createStream({camera: {video: true, audio: true}});
                const localVideo = document.getElementById('local-video');
                
                let streamID = new Date().getTime().toString();
                localVideo.srcObject = localStream;
                zg.startPublishingStream(streamID, localStream);
            }
        });
        
        zg.setSoundLevelDelegate(true, 1000);
        zg.on("soundLevelUpdate", (soundLevelList)=>{
            console.warn("[soundLevelUpdate]",JSON.stringify(soundLevelList))
        });

        zg.on("capturedSoundLevelUpdate", (soundLevel)=>{
            console.warn("[capturedSoundLevelUpdate]",JSON.stringify(soundLevel))
        });
        
        zg.on("deviceError", async (errorCode, deviceName)=>{
            let microPhone = (await zg.getMicrophones()).filter((item)=> item.deviceID == 'default')[0];
            console.error("[deviceError]", errorCode, deviceName, microPhone);
            let rst = await zg.useAudioDevice(localStream, microPhone.deviceID);
            console.error('rst', rst);
        });

        // 加入房间
        fetch(`https://wsliveroom-alpha.zego.im:8282/token?app_id=${appID}&id_name=${userID}`)
            .then(rsp=> rsp.text())
            .then(token=> {
                accessToken = token
                zg.loginRoom(roomID, token, { userID, userName }, { userUpdate: true });
            });
    </script>
</html>